#! /usr/bin/env python
# coding=utf-8
from .utils import __hmac_sha1__
import time
import base64


def rtmp_publish_url(domain, hub, stream_title, mac, expire):
    path = "/%s/%s?e=%d" % (hub, stream_title, time.time()+expire)
    token = "%s:%s" % (mac.__auth__.access_key, __hmac_sha1__(path, mac.__auth__.secret_key))
    url = "rtmp://%s%s&token=%s" % (domain, path, token)
    return url


def rtmp_play_url(domain, hub, stream_title):
    return "rtmp://%s/%s/%s" % (domain, hub, stream_title)


def hls_play_url(domain, hub, stream_title):
    return "http://%s/%s/%s.m3u8" % (domain, hub, stream_title)


def hdl_play_url(domain, hub, stream_title):
    return "http://%s/%s/%s.flv" % (domain, hub, stream_title)


def snapshot_play_url(domain, hub, stream_title):
    return "http://%s/%s/%s.jpg" % (domain, hub, stream_title)

def get_token(hub, stream_title, mac, expire):
    #构造待签名的 Data
    data = "/%s/%s?e=%d" % (hub, stream_title, time.time()+expire)
    #计算 HMAC-SHA1 签名，并对签名结果做 URL 安全的 Base64 编码
    sign = __hmac_sha1__(data, mac.__auth__.secret_key)
    encodedSign = base64.urlsafe_b64encode(sign)
    #将 Qiniu 标识与 AccessKey、encodedSign 拼接得到管理凭证
    token= "Qiniu " +mac.__auth__.access_key+ ":" + encodedSign

    # path = "/%s/%s?e=%d" % (hub, stream_title, time.time()+expire)
    # token = "%s:%s" % (mac.__auth__.access_key, __hmac_sha1__(path, mac.__auth__.secret_key))
    return token

